﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using BusinessFacade;
using EntityManage;
public partial class Modules_Personal_PerfEvaluateOrgPersonalSeasonDetail : System.Web.UI.Page
{
    IReportHandler rptHandler = new ReportManage();
    ISys_UserHandler userHandler = new Sys_UserManage();
    IPerf_Personal_ApprovalHandler approvalHandler = new Perf_Personal_ApprovalManage();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["retu"] = Request.UrlReferrer.ToString(); 
            if (Request["UserId"] != null && Request["UserId"] != string.Empty)
            {
                ViewState["UserId"] = Request["UserId"].ToString();
            }
            if (Request["Season"] != null && Request["Season"] != string.Empty)
            {
                ViewState["Season"] = Request["Season"].ToString();
            }
            ViewState["NowYear"] = DateTime.Now.Year;
            List<SqlParameter[]> parms = null;
            string clause = " 1=1 ";

            ProcessParms(ref clause, ref parms);
            dataBind(clause, parms);
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {

    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(ViewState["retu"].ToString()); 
    }
    protected void gvPersonalList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "GetByScore")
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), System.Guid.NewGuid().ToString(), string.Format("window.open('PerfEvaluateOrgPersonalAllSeasonReport.aspx?UserId={0}')", e.CommandArgument.ToString()), true);
        }
    }
    protected void gvPersonalList_Sorting(object sender, GridViewSortEventArgs e)
    {

    }
    protected void gvPersonalList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

        }
    }
    /// <summary>
    /// 绑定数据源
    /// </summary>
    private void dataBind(string whereClause, List<SqlParameter[]> parms)
    {
        DataTable dtNew = new DataTable();
        dtNew.Columns.Add("季度");
        //绩效得分dt
        DataTable dtPerf = new DataTable();
        DataTable dtTarget = new DataTable();
        if (whereClause != string.Empty)
        {
            SqlParameter[] dtPerfParms = null;
            SqlParameter[] dtTargetParms = null;
            SqlParameter[] dtPerfParms1 = null;
            SqlParameter[] dtTargetParms1 = null;
            dtPerfParms = parms[0];
            dtTargetParms = parms[1];
            dtPerfParms1 = parms[2];
            dtTargetParms1 = parms[3];
            dtPerf = approvalHandler.GetPerf_Personal_ApprovalList(whereClause, dtPerfParms);
            string evaluateId = "0";
            
            DataTable dtTargetType = rptHandler.GetPerTargetTypeTotalGrade("e.id in("+evaluateId+") ", null);
            if (dtPerf != null && dtPerf.Rows.Count > 0)
            {
                for (int i = 0; i < dtPerf.Rows.Count; i++)
                {
                    //添加绩效得分列
                    dtNew.Columns.Add(dtPerf.Rows[i]["TiTle"].ToString());
                    evaluateId += "," + dtPerf.Rows[i]["EvaluateIDs"].ToString();
                }
            }
            if (dtTargetType != null && dtTargetType.Rows.Count > 0)
            {
                for (int j = 0; j < dtTargetType.Rows.Count; j++)
                {
                    //添加指标类型列
                    dtNew.Columns.Add(dtTargetType.Rows[j]["TypeName"].ToString());
                }
            }

            //添加指标列
            string whereTarget = whereClause.Replace("target_user_id","pa.target_user_id");
            whereTarget=whereTarget.Replace("Begin_Time","pa.Begin_Time");
            dtTarget = rptHandler.GetPerTargetScore(" pa.begin_time between @BegionTime and @EndTime  and pa.Target_User_ID= @Target_User_ID ", dtTargetParms);
            //获取唯一的指标名称添加到DataTable
            DataView myDataView = new DataView(dtTarget);
            string[] strComuns ={"TargetName"};
            DataTable dt = myDataView.ToTable(true, strComuns);

            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dtNew.Columns.Add(dt.Rows[i]["TargetName"].ToString());
                }
            }
            //dtNew添加数据
            DataRow row = dtNew.NewRow();
            row["季度"] = ViewState["Season"].ToString();
            if (dtPerf != null && dtPerf.Rows.Count > 0)
            {
                for (int i = 0; i < dtPerf.Rows.Count; i++)
                {
                    //添加绩效得分
                    row[dtPerf.Rows[i]["TiTle"].ToString()] = dtPerf.Rows[i]["Score"];
                    SqlParameter[] targettypeParms = new SqlParameter[1];
                    
                }
            }
            if (dtTargetType != null && dtTargetType.Rows.Count > 0)
            {
                for (int j = 0; j < dtTargetType.Rows.Count; j++)
                {
                    //添加指标类型得分
                    row[dtTargetType.Rows[j]["TypeName"].ToString()] = dtTargetType.Rows[j]["TotalScore"];
                }
            }
            //添加指标列得分
            DataRow dtRow = dt.NewRow();
            if (dt != null && dt.Rows.Count > 0)
            {
                int targetScore=0;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int j = 0; j < dtTarget.Rows.Count; j++)
                    {
                        //当指标名相匹配时，添加分数进去，如果有多个相同的指标名，则相加起来
                        if (dt.Rows[i]["TargetName"].ToString() == dtTarget.Rows[j]["TargetName"].ToString())
                        {
                            try
                            {
                                targetScore += int.Parse(dtTarget.Rows[j]["TotalScore"].ToString());
                            }
                            catch { }
                        }
                    }
                    row[dt.Rows[i]["TargetName"].ToString()] = targetScore.ToString();
                }
            }
            dtNew.Rows.Add(row);
        }
        this.gvPersonalList.DataSource = dtNew;
        gvPersonalList.AutoGenerateColumns = true;
        this.gvPersonalList.DataBind();
    }
    /// <summary>
    /// 条件参数处理
    /// </summary>
    /// <param name="whereClause"></param>
    /// <param name="parms"></param>
    private void ProcessParms(ref string whereClause, ref List<SqlParameter[]> parms)
    {
        List<SqlParameter> lstParm = new List<SqlParameter>();
        List<SqlParameter> lstParm2 = new List<SqlParameter>();
        List<SqlParameter> lstParm3 = new List<SqlParameter>();
        List<SqlParameter> lstParm4 = new List<SqlParameter>();
        parms = new List<SqlParameter[]>();
        //获取当前季度
        if (ViewState["Season"].ToString() == "第一季度")
        {
            whereClause += " and begin_time between @BegionTime and @EndTime ";
            lstParm.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/01/01"));
            lstParm.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/03/31"));
            lstParm2.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/01/01"));
            lstParm2.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/03/31"));
            lstParm3.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/01/01"));
            lstParm3.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/03/31"));
            lstParm4.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/01/01"));
            lstParm4.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/03/31"));
        }
        else if (ViewState["Season"].ToString() == "第二季度")
        {
            whereClause += " and begin_time between @BegionTime and @EndTime ";
            lstParm.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/04/01"));
            lstParm.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/06/30"));
            lstParm2.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/04/01"));
            lstParm2.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/06/30"));
            lstParm3.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/04/01"));
            lstParm3.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/06/30"));
            lstParm4.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/04/01"));
            lstParm4.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/06/30"));
        }
        else if (ViewState["Season"].ToString() == "第三季度")
        {
            whereClause += " and begin_time between @BegionTime and @EndTime ";
            lstParm.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/07/01"));
            lstParm.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/09/30"));
            lstParm2.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/07/01"));
            lstParm2.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/09/30"));
            lstParm3.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/07/01"));
            lstParm3.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/09/30"));
            lstParm4.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/07/01"));
            lstParm4.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/09/30"));
        }
        else if (ViewState["Season"].ToString() == "第四季度")
        {
            whereClause += " and begin_time between @BegionTime and @EndTime ";
            lstParm.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/10/01"));
            lstParm.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/12/31"));
            lstParm2.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/10/01"));
            lstParm2.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/12/31"));
            lstParm3.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/10/01"));
            lstParm3.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/12/31"));
            lstParm4.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/10/01"));
            lstParm4.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/12/31"));
        }
        if (ViewState["UserId"].ToString() != string.Empty)
        {
            whereClause += " and Target_User_ID= @Target_User_ID ";
            lstParm.Add(new SqlParameter("@Target_User_ID", ViewState["UserId"]));
            lstParm2.Add(new SqlParameter("@Target_User_ID", ViewState["UserId"]));
            lstParm3.Add(new SqlParameter("@Target_User_ID", ViewState["UserId"]));
            lstParm4.Add(new SqlParameter("@Target_User_ID", ViewState["UserId"]));
        }
        parms.Add(lstParm.ToArray());
        parms.Add(lstParm2.ToArray());
        parms.Add(lstParm3.ToArray());
        parms.Add(lstParm4.ToArray());
    }
}
